/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springblade.core.mp.base.BaseEntity;
import org.springblade.core.tenant.mp.TenantEntity;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.admin.dto.EstateWeeklyReportConfigAddDTO;
import org.springblade.modules.admin.dto.EstateWeeklyReportConfigListDTO;
import org.springblade.modules.admin.entity.EstateBuildingWeeklyReportUser;
import org.springblade.modules.admin.vo.EstateBuildingWeeklyReportUserVO;
import org.springblade.modules.admin.mapper.EstateBuildingWeeklyReportUserMapper;
import org.springblade.modules.admin.service.IEstateBuildingWeeklyReportUserService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.modules.admin.vo.EstateWeeklyReportConfigListVO;
import org.springblade.modules.admin.vo.UserReportConfigListVO;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 项目周报人员配置 服务实现类
 *
 * @author ysq
 * @since 2024-09-10
 */
@Service
public class EstateBuildingWeeklyReportUserServiceImpl extends BaseServiceImpl<EstateBuildingWeeklyReportUserMapper, EstateBuildingWeeklyReportUser> implements IEstateBuildingWeeklyReportUserService {

	private IUserService userService;

	@Autowired
	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	@Override
	public IPage<EstateWeeklyReportConfigListVO> configGetList(IPage<EstateWeeklyReportConfigListVO> page, EstateWeeklyReportConfigListDTO estateWeeklyReportConfigListDTO) {
		IPage<EstateWeeklyReportConfigListVO> estateWeeklyReportConfigListVOIPage = baseMapper.configGetList(page, estateWeeklyReportConfigListDTO);
		List<EstateWeeklyReportConfigListVO> records = estateWeeklyReportConfigListVOIPage.getRecords();
		if(!Func.isEmpty(records)){
			records.forEach(record -> {
				String configUserIds = record.getConfigUserIds();
				if(!Func.isNull(configUserIds)){
					String[] split = configUserIds.split(",");
					List<String> userIdList = Arrays.asList(split);
					List<Long> idList = userIdList.stream().map(
						Long::valueOf
					).collect(Collectors.toList());
					List<UserReportConfigListVO> userReportConfigListVOList = new ArrayList<>();
					idList.forEach(userId -> {
						User user = userService.getById(userId);
						if(!Func.isNull(user)){
							UserReportConfigListVO userReportConfigListVO = new UserReportConfigListVO();
							userReportConfigListVO.setUserId(userId);
							userReportConfigListVO.setRealName(user.getRealName());
							userReportConfigListVOList.add(userReportConfigListVO);
						}
					});
					record.setUserReportConfigListVOS(userReportConfigListVOList);
					record.setConfigUserIdsList(userIdList);
				}
			});
		}

		estateWeeklyReportConfigListVOIPage.setRecords(records);
		return estateWeeklyReportConfigListVOIPage;

	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public Boolean configSave(EstateWeeklyReportConfigAddDTO estateWeeklyReportConfigAddDTO) {
		Long buildingId = estateWeeklyReportConfigAddDTO.getBuildingId();
		// 查询原有配置列表，若部位空，先删除，后插入
		List<EstateBuildingWeeklyReportUser> existList = this.list(
				new LambdaQueryWrapper<EstateBuildingWeeklyReportUser>()
					.eq(EstateBuildingWeeklyReportUser::getBuildingId, buildingId)
					.eq(BaseEntity::getIsDeleted,0)
					.eq(TenantEntity::getTenantId, estateWeeklyReportConfigAddDTO.getTenantId())
		);
		if(!Func.isEmpty(existList)){
			this.removeByIds(existList.stream().map(BaseEntity::getId).collect(Collectors.toList()));
		}
		// 创建新配置列表
		List<Long> userIds = estateWeeklyReportConfigAddDTO.getUserIds();
		List<EstateBuildingWeeklyReportUser> configList = new ArrayList<>();
		if(!Func.isEmpty(userIds)){
			for (Long userId : userIds) {
				EstateBuildingWeeklyReportUser config = new EstateBuildingWeeklyReportUser();
				config.setBuildingId(buildingId);
				config.setUserId(userId);
				config.setTenantId(estateWeeklyReportConfigAddDTO.getTenantId());
				configList.add(config);
			}
		}
		// 批量插入新配置
		if(!Func.isEmpty(configList)){
			return this.saveBatch(configList);
		}
		return Boolean.TRUE;
	}
}
